package love.yuanmengrobot.qianyugarden.controller;

import love.yuanmengrobot.qianyugarden.entity.CarouselContent;
import love.yuanmengrobot.qianyugarden.entity.RotationArea;
import love.yuanmengrobot.qianyugarden.service.CarouselContentService;
import love.yuanmengrobot.qianyugarden.service.RotationAreaService;
import love.yuanmengrobot.qianyugarden.service.YuanwenService;
import love.yuanmengrobot.qianyugarden.utils.AppFileUtils;
import love.yuanmengrobot.qianyugarden.utils.RandomUtils;
import love.yuanmengrobot.qianyugarden.utils.ResultObj;
import love.yuanmengrobot.qianyugarden.utils.SystemConstant;
import love.yuanmengrobot.qianyugarden.vo.RotationAreaAndcontentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 轮播区(RotationArea)表控制层
 *
 * @author CHJY
 * @since 2021-07-13 14:00:41
 */
@Controller
@RequestMapping("rotationArea")
public class RotationAreaController {
    /**
     * 服务对象
     */
    @Autowired
    RotationAreaService rotationAreaService;
    @Autowired
    CarouselContentService carouselContentService;
    @Autowired
    YuanwenService yuanwenService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    public RotationArea selectOne(String id) {
        return this.rotationAreaService.queryById(id);
    }

    /*
    页面跳转
     */
    @RequestMapping("toBackstageNew")
    public String toBackstageNew(HttpServletRequest request, ModelMap map) {
        map.put("yuanwen", yuanwenService.queryAllByLimit(0, 7));
        map.put("carouselContent", carouselContentService.queryAllByLimit(0, 7));
        List<RotationAreaAndcontentVo> rotationAreaAndcontentVos = rotationAreaService.queryAllByLimitByR(0, 7);
        if (rotationAreaAndcontentVos != null) {
            for (RotationAreaAndcontentVo s : rotationAreaAndcontentVos) {
                for (CarouselContent c : s.getCarouselContentList()) {
//                    System.out.println("查询结果：" + c.toString());
                }
            }
        }

        map.put("rotationArea", rotationAreaAndcontentVos);
        map.put("css_new", "/css/backstage/new.css");
        map.put("css_dia", "/css/backstage/Dialog.css");
        map.put("js_new", "/js/backstage/new.js");
        map.put("js_dia", "/js/backstage/Dialog.js");


        return "backstage/backstageToNew";
    }

    @PostMapping("/addRo")
    @ResponseBody
    public ResultObj addRo(RotationArea rotationArea) {
        try {
            rotationAreaService.insertRotationArea(rotationArea);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping(value = "/uploadImgAndAddYw")
    @ResponseBody
    public ResultObj uploadImgAndAddYw(MultipartFile img, HttpServletRequest request) {
        if (img != null) {
            System.out.println("1:" + img.getName() + img.getSize());
            System.out.println("2:" + request.getParameter("objectid"));
            System.out.println("3:" + request.getParameter("rotationid"));
            String filename = null;
            try {
                String savePath = ResourceUtils.getURL("classpath:").getPath() + "static/img/upimg/";
                File savePathFile = new File(savePath);
                if (!savePathFile.exists()) {
                    savePathFile.mkdir();
                }
                filename = AppFileUtils.createBookFileName(img.getName() + ".png");
                img.transferTo(new File(savePath + filename));

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                CarouselContent carouselContent = new CarouselContent();
                carouselContent.setIconhref("img/upimg/" + filename);
                carouselContent.setRelationid(request.getParameter("ywobjectid"));
                carouselContent.setRotationareaid(request.getParameter("rotationid"));
//                System.out.println(carouselContent.toString());
                carouselContentService.insertCarouselContent(carouselContent);


                return new ResultObj(SystemConstant.OK, "保存成功");
            } catch (Exception e) {
                return new ResultObj(SystemConstant.OK, "保存失败");
            }


        } else System.out.println("null!");

        return new ResultObj(SystemConstant.OK, "保存成功");

    }

    @RequestMapping(value = "/updateRotationArea")
    @ResponseBody
    public ResultObj updateRotationArea(RotationArea rotationArea) {
        try {
            rotationAreaService.update(rotationArea);
            return new ResultObj(SystemConstant.OK, "修改成功");
        } catch (Exception e) {
            return new ResultObj(SystemConstant.OK, "修改失败");
        }


    }
    @RequestMapping(value = "/deleteCarouselContent")
    @ResponseBody
    public ResultObj deleteCarouselContent(String  objectid) {
        try {
            carouselContentService.deleteById(objectid);
            return new ResultObj(SystemConstant.OK, "删除成功");
        } catch (Exception e) {
            return new ResultObj(SystemConstant.OK, "删除失败");
        }


    }

}
